<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo-area"  >
        <i class="iconfont icon-yueliang" style="font-size: 30px" >
        </i>
        <h4>冰轮网盘</h4>
      </div>

      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              class="login-btn"
              @click="handleLogin"
              style="width: 100px;margin: 0 30px;"
          >
            登录
          </el-button>
          <el-button
              type="primary"
              class="login-btn"
              style="width: 100px;margin: 0 30px;"
              @click="toRegister"
          >
            去注册
          </el-button>
        </el-form-item>

        <div class="login-footer">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <el-link type="primary" @click="showForgetDialog">忘记密码?</el-link>
        </div>
      </el-form>
    </div>

    <!-- 忘记密码对话框 -->
    <el-dialog
        title="找回密码"
        :visible.sync="forgetDialogVisible"
        width="30%"
    >
      <el-form>
        <el-form-item label="注册邮箱">
          <el-input placeholder="请输入注册邮箱"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="forgetDialogVisible = false">取消</el-button>
        <el-button type="primary">发送重置邮件</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {loginApi} from "@/request/request";
import {DEV_MOON_TOKEN, USER_INFO} from "@/constant/ConStant";

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ]
      },
      rememberMe: false,
      loading: false,
      forgetDialogVisible: false
    }
  },
  methods: {
    toRegister(){
      this.$router.push({path:'/register'})
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          loginApi(this.loginForm).then(e=>{

            if(e['data']['code'] === 200){
              localStorage.setItem(USER_INFO,JSON.stringify(e['data']['data'] ) )
              localStorage.setItem(DEV_MOON_TOKEN,e['data']['data']['tokenValue']   )
              this.$message({
                type: 'success',
                message: '登录成功'
              })
             this.$router.push({
               path:'/home/file'
             })
            }else{
              this.$message({
                type:'info',
                message:"登录失败 " + e['data']['msg']
              })
            }




          })


        }
      })
    },
    showForgetDialog() {
      this.forgetDialogVisible = true
    }
  }
}
</script>

<style scoped>

.pointer-folder-style{
  cursor: pointer;
}

.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  background-size: cover;
}

.login-box {
  width: 320px;
  padding: 30px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.login-box:hover {
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.logo-area {
  text-align: center;
  margin-bottom: 30px;
}

.logo-area img {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.logo-area h1 {
  font-size: 24px;
  color: #409EFF;
  margin: 0;
  font-weight: 500;
}

.login-form {
  margin-top: 20px;
}

.login-btn {
  width: 80%;
  height: 35px;
  font-size: 13px;
  letter-spacing: 1px;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.el-checkbox {
  color: #909399;
}

.el-link {
  font-size: 14px;
}
</style>